package com.chandra.pos.employee.web;


import com.chandra.pos.common.web.POSController;
import com.chandra.pos.employee.model.EmployeeFilter;
import com.chandra.pos.employee.model.EmployeeList;
import com.chandra.pos.employee.model.EmployeeModel;
import com.chandra.pos.employee.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityNotFoundException;

/**
 * Created by Admin on 7/2/14.
 */
@Controller
@RequestMapping("employee")
public class EmployeeController extends POSController {


    public EmployeeController() {
        this.setTabIndex(4);

    }

    Logger logger = LoggerFactory.getLogger(EmployeeController.class);


    private static String PAGE_MODULE = "employee";
    private static String PAGE_NEW_PRODUCT = "newemployee";

    private static String PAGE_ACCOUNT_SETTINGS = "settings";

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public String index(EmployeeModel model) {

        return getPageURL(PAGE_MODULE, PAGE_HOME);

    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(EmployeeModel model) {

        return getPageView(PAGE_MODULE, PAGE_HOME);

    }

    @RequestMapping(value = "/settings", method = RequestMethod.GET)
    public String setting(EmployeeModel model) {

        return getPageView(PAGE_MODULE, PAGE_ACCOUNT_SETTINGS);

    }


    @RequestMapping(value = "search", method = RequestMethod.GET)
    public
    @ResponseBody
    EmployeeList search(EmployeeFilter filter) {


        return employeeService.searchEmployee(filter);


    }

    @RequestMapping(value = "checkUserName", method = RequestMethod.GET)
    public
    @ResponseBody
    EmployeeModel checkUserName(EmployeeFilter filter) {

        EmployeeModel employeeModel = new EmployeeModel();
        try {

            employeeModel = employeeService.getByUserName(filter.getQuery());
        } catch (EntityNotFoundException e) {

        }

        return employeeModel;
    }


    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String newProduct(EmployeeModel model) {

        return getPageView(PAGE_MODULE, PAGE_NEW_PRODUCT);

    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public
    @ResponseBody
    EmployeeModel save(EmployeeModel productModel) {


        productModel = employeeService.saveEmployee(productModel);
        return productModel;

    }
    @RequestMapping(value = "getDetails", method = RequestMethod.GET)
    public
    @ResponseBody
    EmployeeModel getDetails(EmployeeFilter filter) {

        EmployeeModel employeeModel = new EmployeeModel();
        try {

            employeeModel = employeeService.getEmployeeDetail(filter);
        } catch (EntityNotFoundException e) {

        }

        return employeeModel;
    }
}